// ==UserScript==
// @name         网络请求监听与内容显示脚本
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  监听包含 'aisubtitle' 的网络请求，并在页面上的显示框中按行显示 content 内容
// @author       您的名字
// @match        https://www.bilibili.com/video/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var summary_promote = '以下是一段视频语音转换文本的内容,需要你扮演一名知识渊博的学者对于其进行总结,并且对于视频中的知识点进行记录,并且以一二三四的方式一一列出，如果作者有在视频中提到任何的工具，网址，予以记录，最后总结出作者的核心思想与观点。文本如下:'

    function createBox() {
        var box = document.createElement('div');
        box.style.position = 'fixed';
        box.style.right = '500px';
        box.style.top = 'calc(50% - 200px)'; // 通过 calc 减去半个 box 的高度
        box.style.width = '300px';
        box.style.height = '400px';
        box.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
        box.style.zIndex = '9999';
        box.style.padding = '0';
        box.style.borderRadius = '5px';
        box.style.color = 'white';
        box.style.whiteSpace = 'pre-line';
        document.body.appendChild(box);
        return box;
    }

    function createTitleBar(box) {
        var titleBar = document.createElement('div');
        titleBar.style.width = '100%';
        titleBar.style.height = '30px';
        titleBar.style.backgroundColor = '#3498db';
        titleBar.style.color = 'white';
        titleBar.style.display = 'flex';
        titleBar.style.alignItems = 'center';
        titleBar.style.cursor = 'move';
        titleBar.style.zIndex = '10000';
        box.insertBefore(titleBar, box.firstChild);

        // 创建“AI总结”按钮
        var summaryButton = createButton(titleBar, 'AI总结');
        summaryButton.onclick = function() {
            // 执行打开“AI总结”面板的代码
            console.log('AI总结面板打开');

            console.log(contentBox.textContent.replace(/[\n\s]+/g, ''))

            //contentBox.textContent += item.content + '\n';
        };

        // 创建“设置”按钮
        var settingsButton = createButton(titleBar, '设置');
        settingsButton.onclick = function() {
            // 执行打开“设置”面板的代码
            console.log('设置面板打开');
        };

        return titleBar;
    }

    function createToggleButton(titleBar, box, contentBox) {
        var toggleButton = document.createElement('button');
        toggleButton.textContent = '☰';
        toggleButton.style.position = 'absolute'; // 使用绝对定位
        toggleButton.style.left = '10px'; // 从左边缘开始
        toggleButton.style.top = '5px'; // 从顶边缘稍微开始
        toggleButton.style.padding = '5px 10px';
        toggleButton.style.fontSize = '16px';
        toggleButton.style.color = '#ecf0f1';
        toggleButton.style.backgroundColor = 'transparent';
        toggleButton.style.border = 'none';
        toggleButton.style.cursor = 'pointer';
        titleBar.appendChild(toggleButton);
        toggleButton.addEventListener('click', function() {
            var isExpanded = box.style.height === '400px';
            box.style.height = isExpanded ? '30px' : '400px';
            contentBox.style.display = isExpanded ? 'none' : 'block';
            // 更新 top 值以适应新的高度
            box.style.top = isExpanded ? 'calc(50% - 15px)' : 'calc(50% - 200px)';
        });
        return toggleButton;
    }

    function createButton(titleBar, text) {
        var button = document.createElement('button');
        button.textContent = text;
        button.style.padding = '5px 15px';
        button.style.marginLeft = '10px';
        button.style.fontSize = '14px';
        button.style.color = 'white';
        button.style.backgroundColor = '#3498db';
        button.style.border = 'none';
        button.style.borderRadius = '3px';
        button.style.cursor = 'pointer';
        button.style.transition = 'background-color 0.3s';
        button.onmouseover = function() {
            button.style.backgroundColor = '#5dade2';
        };
        button.onmouseout = function() {
            button.style.backgroundColor = '#3498db';
        };
        titleBar.appendChild(button);
        return button;
    }

    function createContentBox(box) {
        var contentBox = document.createElement('div');
        contentBox.style.overflowY = 'auto';
        contentBox.style.height = '370px'; // 使内容盒子的高度减去标题栏的高度
        contentBox.style.borderTop = '1px solid #3498db'; // 给内容盒子顶部加一条边框，以视觉上分隔标题栏
        box.appendChild(contentBox);
        return contentBox;
    }

    function enableDrag(titleBar, box) {
        // 将拖动功能附加到 titleBar 上，而不是整个 box
        titleBar.onmousedown = function(event) {
            event.preventDefault();
            var shiftX = event.clientX - box.getBoundingClientRect().left;
            var shiftY = event.clientY - box.getBoundingClientRect().top;
            function moveAt(pageX, pageY) {
                box.style.left = pageX - shiftX + 'px';
                box.style.top = pageY - shiftY + 'px';
            }
            function onMouseMove(event) {
                moveAt(event.pageX, event.pageY);
            }
            document.addEventListener('mousemove', onMouseMove);
            document.onmouseup = function() {
                document.removeEventListener('mousemove', onMouseMove);
            };
        };
        titleBar.ondragstart = function() {
            return false;
        };
    }

    //重写fetch 拦截b站ai字幕
    function overrideFetch(contentBox) {
        var originalFetch = window.fetch;
        window.fetch = function() {
            var fetchArguments = arguments;
            var url = fetchArguments[0];
            var fetchPromise = originalFetch.apply(this, fetchArguments);
            if (typeof url === "string" && url.includes('aisubtitle')) {
                fetchPromise.then(function(response) {
                    return response.clone().json();
                }).then(function(data) {
                    if (data.body && Array.isArray(data.body)) {
                        data.body.forEach(function(item) {
                            contentBox.textContent += item.content + '\n';
                        });
                    }
                }).catch(function(error) {
                    contentBox.textContent += '错误: ' + error + '\n';
                });
            }
            return fetchPromise;
        };
    }

    var box = createBox();
    var titleBar = createTitleBar(box);
    var contentBox = createContentBox(box);
    var toggleButton = createToggleButton(titleBar, box, contentBox);
    enableDrag(titleBar, box);
    overrideFetch(contentBox);
})();
